﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YAMP;

namespace SayisalAnaliz.Library
{
    public static class SimpsonYontemi
    {

        public static double Uygula(double altDeger, double ustDeger, int parca, string fonksiyon)
        {
            fonksiyon = fonksiyon.Replace(',', '.');
            double h = (ustDeger - altDeger) / parca;

            double f0 = ((ScalarValue)Parser.Parse(fonksiyon).Execute(new { x = altDeger })).Value;
            double fn = ((ScalarValue)Parser.Parse(fonksiyon).Execute(new { x = ustDeger })).Value;
            double fi = 0;
            
            for (int i = 1; i < parca; i++)
            {
                if (i % 2 == 0)
                    fi += 2.0 * ((ScalarValue)Parser.Parse(fonksiyon).Execute(new { x = altDeger + h * i })).Value;
                if (i % 2 == 1)
                    fi += 4.0 * ((ScalarValue)Parser.Parse(fonksiyon).Execute(new { x = altDeger + h * i})).Value;
            }
            double sonuc = (h/3) * (f0 + fn + fi);
            return sonuc;

        }
    }
}
